import axios from 'axios';
import apiConfig from "../config/api.config";

const  httpRequest = axios.create({
  timeout: 1000 * 30,
  withCredentials: false,
  baseURL: apiConfig.baseUrl,// 配置接口地址
  headers: {
    'Content-Type': 'application/json; charset=utf-8'
  }
})

/**
 * 请求拦截
 */
httpRequest.interceptors.request.use(config => {
  config.headers['token'] ="im token 1231322" // 请求头带上token
  console.log('请求拦截:请求成功')
  return config
}, error => {
  console.log('请求拦截:请求失败')
  return Promise.reject(error)
})

/**
 * 响应拦截
 */
httpRequest.interceptors.response.use(response => {
  if (response.data && response.data.code === 200) {
        console.log('响应拦截:响应成功')

  }else if(response.data && response.data.code === 401){
    // 401, token失效
  }
  return response.data
}, error => {
  console.log('响应拦截:响应失败')
  return Promise.reject(error)
})
export default httpRequest

